﻿using System;
using System.Collections.Generic;
using System.Linq;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsPageFrontEndDataCommand : CmsPageFrontEndCommand
    {
        public override CmsPage ExecuteOne()
        {
            CmsPage result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPage>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPage>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPage, int>>();

                result = SqlUtility<CmsPage, int>.ExecuteMultiReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_PageGetFrontEndPage",
                        new
                        {
                            SiteId = CommandParameter.SiteId,
                            SiteGroupId = CommandParameter.SiteGroupId,
                            SystemLanguageId = CommandParameter.SystemLanguageId,
                            PageUrl = CommandParameter.PageUrl,
                            PageTypeParameter = "Page", //TO DO - remove hardcoded value,
                            PageControlTypeParameter = "PageControl" //TO DO - remove hardcoded value
                        },
                        mapper);
            }

            return result;
        }
    }
}
